﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WoWRealmlistChanger.Properties;
using System.Windows.Forms;
using System.IO;
using WoWRealmlistChanger.Entities;
using System.Diagnostics;
using WoWRealmlistChanger.Model;

namespace WoWRealmlistChanger.Controller
{
    class WowController
    {
        Settings m_Settings;

        public StarterModel ViewData { get; set; }

        StarterFields m_StarterFields;

        public WowController()
        {
            m_Settings = Settings.Default;

            ViewData = new StarterModel();

            PushConfigToModel();

            m_StarterFields = new StarterFields();
        }

        internal void PushConfigToModel()
        {
            ViewData.ChkClose = m_Settings.m_ChkClose;

            ViewData.ChkDeleteCache = m_Settings.m_chkCache;

            ViewData.LstBc = m_Settings.m_BcRealmlists;

            ViewData.LstWotlk = m_Settings.m_WotlkRealmlists;

            ViewData.LstIndexWotlk = m_Settings.m_WotlkListIndex;

            ViewData.LstIndexBc = m_Settings.m_BcListIndex;
        }

        internal void PushModelToConfig()
        {
            m_Settings.m_ChkClose = ViewData.ChkClose;

            m_Settings.m_chkCache = ViewData.ChkDeleteCache;

            m_Settings.m_BcRealmlists = ViewData.LstBc;

            m_Settings.m_WotlkRealmlists = ViewData.LstWotlk;

            m_Settings.m_WotlkListIndex = ViewData.LstIndexWotlk;

            m_Settings.m_BcListIndex = ViewData.LstIndexBc;

            m_Settings.Save();
        }

        internal void ChooseTbcFolder()
        {
            using (System.Windows.Forms.FolderBrowserDialog objDialog = new FolderBrowserDialog())
            {
                objDialog.Description = "Bitte wählen Sie den Ordner aus in dem sich die 2.4.3 WoW.exe befindet";
                objDialog.SelectedPath = @"C:\";       // Vorgabe Pfad (und danach der gewählte Pfad)
                DialogResult objResult = objDialog.ShowDialog();
                if (objResult == DialogResult.OK)
                {
                    MessageBox.Show("Neuer WoW Pfad : " + objDialog.SelectedPath);
                    Settings.Default.m_BcPath = objDialog.SelectedPath;
                }
                else
                    MessageBox.Show("Auswahl abgebrochen");
            }
        }

        internal void StartTbc()
        {
            try
            {
                if (File.Exists(m_Settings.m_BcPath + m_StarterFields.m_WowExe))
                {
                    //Delete Cache Folder
                    if (Directory.Exists(m_Settings.m_BcPath + m_StarterFields.m_CacheFolder) && m_Settings.m_chkCache == true)
                    {
                        Directory.Delete(m_Settings.m_BcPath + m_StarterFields.m_CacheFolder, true);
                    }
                    //Set Realmlist
                    ChangeRealmlist("bc");
                    //Start Burning Crusade WoW
                    Process.Start(m_Settings.m_BcPath + m_StarterFields.m_WowExe);
                    ExitApplication();
                }
                else
                    MessageBox.Show("WoW.exe wurde am angegebenen Ort nicht gefunden");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in StartTbc: {0}", ex.Message);
            }
        }



        internal void AddToBcList()
        {
            if (!ViewData.LstBc.Contains(ViewData.TxtBc))
            {
                ViewData.LstBc.Add(ViewData.TxtBc);
                ViewData.TxtBc = null;
            }
        }

        internal void RemoveFromBcList()
        {
            if (ViewData.LstIndexBc != -1)
                ViewData.LstBc.RemoveAt(ViewData.LstIndexBc);
        }

        internal void AddToWotlkList()
        {
            if (!ViewData.LstWotlk.Contains(ViewData.TxtWotlk))
            {
                ViewData.LstWotlk.Add(ViewData.TxtWotlk);
                ViewData.TxtWotlk = null;
            }
        }

        internal void RemoveFromWotlkList()
        {
            if (ViewData.LstIndexWotlk != -1)
                ViewData.LstWotlk.RemoveAt(ViewData.LstIndexWotlk);
        }

        internal void ExitApplication()
        {
            PushModelToConfig();

            if (ViewData.ChkClose == true)
            {
                Environment.Exit(0);
            }
        }

        internal void ChangeRealmlist(string version)
        {
            try
            {
                if (version == "wotlk")
                {
                    foreach (string language in m_StarterFields.m_Languages)
                    {
                        if (File.Exists(m_Settings.m_WotlkPath + language))
                        {
                            using (StreamWriter writer = new StreamWriter(m_Settings.m_WotlkPath + language))
                            {
                                writer.WriteLine("set realmlist " + ViewData.LstWotlkSelectedValue);
                            }
                        }

                    }
                }
                else if (version == "bc")
                {
                    using (StreamWriter writer = new StreamWriter(m_Settings.m_BcPath + m_StarterFields.m_RealmlistWtf))
                    {
                        writer.WriteLine("set realmlist " + ViewData.LstBcSelectedValue);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in ChangeRealmlist: {0}", ex.Message);
            }
        }

        internal void ResetConfig()
        {
            m_Settings.Reset();
            PushConfigToModel();
            m_Settings.Upgrade();
            System.Windows.MessageBox.Show("Configuration File was reseted");
        }

        /// <summary>
        /// Choose Wotlk folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal void ChooseWotlkFolder()
        {
            using (System.Windows.Forms.FolderBrowserDialog objDialog = new FolderBrowserDialog())
            {
                objDialog.Description = "Bitte wählen Sie den Ordner aus in dem sich die 3.3.5 WoW.exe befindet";
                objDialog.SelectedPath = @"C:\";       // Vorgabe Pfad (und danach der gewählte Pfad)
                DialogResult objResult = objDialog.ShowDialog();
                if (objResult == DialogResult.OK)
                {
                    MessageBox.Show("Neuer WoW Pfad : " + objDialog.SelectedPath);
                    m_Settings.m_WotlkPath = objDialog.SelectedPath;
                }
                else
                    MessageBox.Show("Abbruch gewählt!");
            }
        }

        internal void StartWotlk()
        {
            try
            {
                if (File.Exists(m_Settings.m_WotlkPath + m_StarterFields.m_WowExe))
                {
                    //Delete Cache Folder
                    if (Directory.Exists(m_Settings.m_WotlkPath) && m_Settings.m_chkCache == true)
                    {
                        Directory.Delete(m_Settings.m_WotlkPath + m_StarterFields.m_CacheFolder, true);
                    }
                    //Change Realmlist
                    ChangeRealmlist("wotlk");
                    //Start Burning Crusade WoW
                    Process.Start(m_Settings.m_WotlkPath + m_StarterFields.m_WowExe);
                    ExitApplication();
                }
                else
                    MessageBox.Show("WoW.exe wurde am angegebenen Ort nicht gefunden");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in StartWotlk: {0}", ex.Message);
            }
        }


    }
}
